﻿using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace IInject.Reflection
{
	public class ParameterCollection : IEnumerable<Parameter>
	{
		#region Fields

		private List<Parameter> _list = new List<Parameter>();

		#endregion

		#region Constructors

		public ParameterCollection(MethodBase method, ParameterProvider provider)
		{
			var parameters = method.GetParameters();

			for (int i = 0; i < parameters.Length; i++)
			{
				var parameter = provider.GetParameter(parameters[i]);
				_list.Insert(parameter.Position, parameter);
			}
		}

		#endregion

		#region Properties

		public Parameter this[int position]
		{
			get
			{
				return _list[position];
			}
		}

		public int Count
		{
			get
			{
				return _list.Count;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return true;
			}
		}

		public MethodBase OwningMethod { get; private set; }

		public ParameterProvider ParameterProvider { get; private set; }

		#endregion

		#region Methods

		public int IndexOf(Parameter parameter)
		{
			if (Contains(parameter))
			{
				return parameter.Position;
			}

			return -1;
		}

		public bool Contains(Parameter parameter)
		{
			return _list.Contains(parameter);
		}

		public IEnumerator<Parameter> GetEnumerator()
		{
			return _list.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion
	}
}
